package com.zenithmind.document.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

/**
 * 文档收藏创建DTO
 * 遵循单一职责原则：专注于收藏创建数据传输
 * 遵循开闭原则：通过验证注解支持扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档收藏创建DTO")
public class DocumentFavoriteCreateDTO {

    @Schema(description = "文档ID", required = true, example = "doc123456")
    @NotBlank(message = "文档ID不能为空")
    private String documentId;

    @Schema(description = "用户ID", required = true, example = "user123456")
    @NotBlank(message = "用户ID不能为空")
    private String userId;

    @Schema(description = "收藏夹ID", example = "folder123456")
    private String folderId;

    @Schema(description = "收藏夹名称", example = "我的收藏")
    @Size(max = 100, message = "收藏夹名称不能超过100字符")
    private String folderName;

    @Schema(description = "收藏夹描述", example = "重要文档收藏夹")
    @Size(max = 500, message = "收藏夹描述不能超过500字符")
    private String folderDescription;

    @Schema(description = "收藏夹颜色", example = "#FF5722")
    private String folderColor;

    @Schema(description = "收藏夹图标", example = "fa-star")
    private String folderIcon;

    @Schema(description = "标签", example = "重要,学习,参考")
    @Size(max = 200, message = "标签不能超过200字符")
    private String tags;

    @Schema(description = "备注", example = "这是一个重要的参考文档")
    @Size(max = 1000, message = "备注不能超过1000字符")
    private String remarks;

    @Schema(description = "是否公开", example = "true")
    private Boolean isPublic;

    @Schema(description = "排序权重", example = "100")
    private Integer sortWeight;

    @Schema(description = "状态", example = "ACTIVE", allowableValues = {"ACTIVE", "INACTIVE"})
    private String status;

    @Schema(description = "扩展属性", example = "{\"priority\": \"high\"}")
    private String extendedProperties;

    /**
     * 创建简单收藏
     */
    public static DocumentFavoriteCreateDTO createSimpleFavorite(String documentId, String userId) {
        DocumentFavoriteCreateDTO dto = new DocumentFavoriteCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setIsPublic(false);
        dto.setSortWeight(100);
        dto.setStatus("ACTIVE");
        return dto;
    }

    /**
     * 创建收藏夹收藏
     */
    public static DocumentFavoriteCreateDTO createFolderFavorite(String documentId, String userId, 
                                                                String folderId, String folderName) {
        DocumentFavoriteCreateDTO dto = createSimpleFavorite(documentId, userId);
        dto.setFolderId(folderId);
        dto.setFolderName(folderName);
        return dto;
    }

    /**
     * 创建带标签的收藏
     */
    public static DocumentFavoriteCreateDTO createTaggedFavorite(String documentId, String userId, 
                                                               String tags, String remarks) {
        DocumentFavoriteCreateDTO dto = createSimpleFavorite(documentId, userId);
        dto.setTags(tags);
        dto.setRemarks(remarks);
        return dto;
    }

    /**
     * 创建公开收藏
     */
    public static DocumentFavoriteCreateDTO createPublicFavorite(String documentId, String userId, 
                                                               String folderName, String tags) {
        DocumentFavoriteCreateDTO dto = createSimpleFavorite(documentId, userId);
        dto.setFolderName(folderName);
        dto.setTags(tags);
        dto.setIsPublic(true);
        return dto;
    }

    /**
     * 验证DTO数据
     */
    public boolean isValid() {
        if (documentId == null || documentId.trim().isEmpty()) return false;
        if (userId == null || userId.trim().isEmpty()) return false;
        
        // 验证收藏夹名称长度
        if (folderName != null && folderName.length() > 100) return false;
        
        // 验证收藏夹描述长度
        if (folderDescription != null && folderDescription.length() > 500) return false;
        
        // 验证标签长度
        if (tags != null && tags.length() > 200) return false;
        
        // 验证备注长度
        if (remarks != null && remarks.length() > 1000) return false;
        
        return true;
    }

    /**
     * 是否为公开收藏
     */
    public boolean isPublicFavorite() {
        return isPublic != null && isPublic;
    }

    /**
     * 是否有收藏夹
     */
    public boolean hasFolder() {
        return folderId != null && !folderId.trim().isEmpty();
    }

    /**
     * 是否有标签
     */
    public boolean hasTags() {
        return tags != null && !tags.trim().isEmpty();
    }

    /**
     * 是否有备注
     */
    public boolean hasRemarks() {
        return remarks != null && !remarks.trim().isEmpty();
    }

    /**
     * 获取标签数组
     */
    public String[] getTagArray() {
        if (tags == null || tags.trim().isEmpty()) {
            return new String[0];
        }
        return tags.split(",");
    }

    /**
     * 获取默认收藏夹名称
     */
    public String getDefaultFolderName() {
        if (folderName != null && !folderName.trim().isEmpty()) {
            return folderName;
        }
        return "默认收藏夹";
    }

    /**
     * 获取默认状态
     */
    public String getDefaultStatus() {
        if (status != null && !status.trim().isEmpty()) {
            return status;
        }
        return "ACTIVE";
    }

    /**
     * 获取默认排序权重
     */
    public Integer getDefaultSortWeight() {
        if (sortWeight != null) {
            return sortWeight;
        }
        return 100;
    }

    /**
     * 获取默认公开状态
     */
    public Boolean getDefaultIsPublic() {
        if (isPublic != null) {
            return isPublic;
        }
        return false;
    }
}
